package com.jrx.anytxn.param.controller.investor;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.GaInvestorSubjectConfigReq;
import com.jrx.anytxn.param.dto.investor.GaInvestorSubjectConfigRes;
import com.jrx.anytxn.param.service.investor.IGaInvestorSubjectConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 出资方科目配置表
 * @author 黄秋平
 * @create 2020/6/15
 */
@Api(tags = "出资方科目配置表api接口")
@RestController
@RequestMapping(value = "/param/investor/ga")
public class GaInvestorSubjectConfigController {

    private static final Logger logger= LoggerFactory.getLogger(GaInvestorSubjectConfigController.class);


    @Resource
    private IGaInvestorSubjectConfigService iGaInvestorSubjectConfigService;

    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和出资方ID查询出资方科目配置表", notes = "根据租户ID和出资方ID查询出资方科目配置表")
    @GetMapping(value = "/{orgId}/{investorId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId",value = "机构ID",required = true),
            @ApiImplicitParam(name = "investorId",value = "出资方ID",required = true)
    })
    public TxnRespResult<GaInvestorSubjectConfigRes> getByTenIdAndInvestorId(@PathVariable String orgId, @NotNull @PathVariable String investorId) {
        TxnRespResult<GaInvestorSubjectConfigRes> result = new TxnRespResult<>();
        GaInvestorSubjectConfigRes res;
        try {
            res = iGaInvestorSubjectConfigService.findByTenIdAndInvestorId(String.valueOf(infoService.getUser().getProjectId()),investorId,orgId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询出资方科目配置表",notes = "根据租户ID分页查询出资方科目配置表")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="页大小",required = true)})
    public TxnRespResult<TxnPage<GaInvestorSubjectConfigRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) throws TxnException {

        TxnRespResult<TxnPage<GaInvestorSubjectConfigRes>> result = new TxnRespResult<>();
        TxnPage<GaInvestorSubjectConfigRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iGaInvestorSubjectConfigService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        }catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "新增出资方科目配置表",notes = "新增出资方科目配置表")
    @PostMapping("/")
    public TxnRespResult<GaInvestorSubjectConfigRes> createInvestorTable(@RequestBody GaInvestorSubjectConfigReq gaInvestorSubjectConfigReq) {
        GaInvestorSubjectConfigRes res;
        TxnRespResult<GaInvestorSubjectConfigRes> result = new TxnRespResult<>();
        try {
            gaInvestorSubjectConfigReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            gaInvestorSubjectConfigReq.setUpdateBy(infoService.getUser().getUserName());
            res = iGaInvestorSubjectConfigService.addInvestorTable(gaInvestorSubjectConfigReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑出资方科目配置表",notes = "编辑出资方科目配置表")
    @PutMapping("/")
    public TxnRespResult<GaInvestorSubjectConfigRes> modifyInvestorTable(@RequestBody GaInvestorSubjectConfigReq gaInvestorSubjectConfigReq) {
        TxnRespResult<GaInvestorSubjectConfigRes> result = new TxnRespResult<>();
        GaInvestorSubjectConfigRes res;
        try {
            gaInvestorSubjectConfigReq.setUpdateBy(infoService.getUser().getUserName());
            res = iGaInvestorSubjectConfigService.modifyInvestorTable(gaInvestorSubjectConfigReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "根据出资方ID和租户ID删除出资方科目配置表",notes = "根据出资方ID和租户ID删除出资方科目配置表")
    @DeleteMapping(value = "/{investorId}/{orgId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "investorId",value = "出资方ID",required = true),
            @ApiImplicitParam(name = "orgId",value = "机构ID",required = true),
    })
    public TxnRespResult<Boolean> removeByIdAndTenId(@NotNull @PathVariable String investorId, @PathVariable("orgId") String orgId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iGaInvestorSubjectConfigService.removeByInvestorIdAndTenId(investorId,String.valueOf(infoService.getUser().getProjectId()),orgId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }
}
